<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 基本信息
 */
class Base extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    /**
    * 作业队更新
    */
    public function unit_update(){
        $bh  = $this->request->request("bh");
        $content2 = $this->request->request("content2");
        if (!$bh || !$content2) {
            $this->error(__('Invalid parameters'));
        }

        $info = Db::name('basedata')->where('Bh',$bh)->field('id')->find();
        if(!$info) {
            $this->error('未查询到当前编号信息');
        }

        $result = Db::name('basedata')->where('id', $info['id'])->update(['Content2' =>$content2]);
        if (!$result) {
            $this->error('更新失败');
        }
        $this->success('更新成功');
    }

    /**
    * 终端及设备列表
    */
    public function terminal_list(){
        $bh  = $this->request->request("bh");
        if (!$bh) {
            $this->error(__('Invalid parameters'));
        }

        // $data = [];
        // //查询
        // $info = Db::name('basedata')->where(['Bh'=>$bh,'Datatype'=>'现场管理机'])->field('id,Content1,Content2')->find();
        // if(!$info) {
        //     $this->error('未查询到终端信息');
        // }

        // $data[0]['id'] = $info['id'];
        // $data[0]['Content1'] = $info['Content1'];
        // $data[0]['Content2'] = $info['Content2'];


        $list = Db::name('basedata')->where(['Partntid'=>$bh,'Datatype'=>'终端设备'])->field('id,Content1,Content2')->select();
        if($list) {
            foreach ($list as $k => $v) {
               $list[$k]['status'] = 1;
               if ($v['Content1'] == '监控终端') {
                    $list[$k]['status'] = 0;
               }
            }
        }

        $this->success('',$list);
    }


    /**
    * 添加终端设备
    */
    public function add_terminal(){
        $bh  = $this->request->request("bh");
        $name  = $this->request->request("name"); //设备类型？/名称
        $no  = $this->request->request("no"); //设备编号
        if (!$bh || !$name || !$no) {
            $this->error(__('Invalid parameters'));
        }

        //不允许终no重复
        $info = Db::name('basedata')->where(['Datatype'=>'终端设备','Content2'=>$no])->find();
        if ($info) {
            $this->error("设备编号已存在");
        }

        $data = [
            'Partntid' => $bh,
            'Datatype' => '终端设备',
            'Bh'       => date('YmdHis').rand(1000,9999),
            'Content1' => $name,
            'Content2' => $no,
        ];

        $result = Db::name('basedata')->insert($data);
        if (!$result) {
            $this->error("添加失败");
        }

        $this->success('添加成功');
    }

    /**
    * 编辑终端设备
    */
    public function edit_terminal(){
        $id  = $this->request->request("id");
        $name  = $this->request->request("name"); //设备类型？/名称
        $no  = $this->request->request("no"); //设备编号
        if (!$id ||!$name || $no) {
            $this->error(__('Invalid parameters'));
        }


        $info = Db::name('basedata')->where('id',$id)->find();
        if (!$info) {
           $this->error("未获取到该终端设备");
        }

        $data = [
            'Content1' => $name,
            'Content2' => $no,
        ];

        $result = Db::name('basedata')->where('id',$id)->update($data);
        if (!$result) {
            $this->error("编辑失败");
        }

        $this->success('编辑成功');
    }

     /**
    * 删除终端设备
    */
    public function del_terminal(){
        $id  = $this->request->request("id");
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }

        $result = Db::name('basedata')->delete($id);
        if (!$result) {
            $this->error("删除失败");
        }

        $this->success('删除成功');
    }


     /**
    * 作业队列表
    */
    public function work_list(){
        $bh  = $this->request->request("bh");
        if (!$bh) {
            $this->error(__('Invalid parameters'));
        }

        $list = Db::name('basedata')->where(['Partntid'=>$bh,'Datatype'=>'施工人员'])->field('id,Content1')->select();
      

        $this->success('',$list);
    }

     /**
    * 添加作业队人员
    */
    public function add_work(){
        $bh  = $this->request->request("bh");
        $name  = $this->request->request("name"); //人员姓名
        if (!$bh || !$name) {
            $this->error(__('Invalid parameters'));
        }

        $data = [
            'Partntid' => $bh,
            'Datatype' => '施工人员',
            'Bh'       => date('YmdHis').rand(1000,9999),
            'Content1' => $name,
        ];

        $result = Db::name('basedata')->insert($data);
        if (!$result) {
            $this->error("添加失败");
        }

        $this->success('添加成功');
    }


    /**
    * 编辑作业队人员
    */
    public function edit_work(){
        $id  = $this->request->request("id");
        $name  = $this->request->request("name"); //设备类型？/名称
        if (!$id ||!$name) {
            $this->error(__('Invalid parameters'));
        }


        $info = Db::name('basedata')->where('id',$id)->find();
        if (!$info) {
           $this->error("未获取到该作业队信息");
        }

        $data = [
            'Content1' => $name,
        ];

        $result = Db::name('basedata')->where('id',$id)->update($data);
        if (!$result) {
            $this->error("编辑失败");
        }

        $this->success('编辑成功');
    }

     /**
    * 删除作业队人员
    */
    public function del_work(){
        $id  = $this->request->request("id");
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }

        $result = Db::name('basedata')->delete($id);
        if (!$result) {
            $this->error("删除失败");
        }

        $this->success('删除成功');
    }
   
}
